package com.wenda.homeschooedu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.wenda.homeschooedu.biz.ScoreManager;
import com.wenda.homeschooedu.model.Score;

public class ScoreServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse responce) throws ServletException, IOException {
		PrintWriter out = null;
		String result = "周雨浓";

		if (request.getParameter("format") != null) {
			if (request.getParameter("format").equals("json")) {
				if (request.getParameter("type").equals("get")) {
					responce.setContentType("text/html;charset=utf-8");
					out = responce.getWriter();
					int class_id = Integer.parseInt(request
							.getParameter("class_id"));
					int year = Integer.parseInt(request.getParameter("year"));
					int term = Integer.parseInt(request.getParameter("term"));
					int exam_id = Integer.parseInt(request
							.getParameter("exam_id"));
					int orderBy = Integer.parseInt(request.getParameter("order"));

					List<Score> scoreMap = new ScoreManager().getStudentScores(
							class_id, year, term, exam_id , orderBy);
					Gson gson = new Gson();
					result = gson.toJson(scoreMap).toString();

				}
				if (result.equals("周雨浓")) {
					result = "查询无记录";
				}
				out.print(result);
				out.flush();
				out.close();
			}
		}

	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse responce) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(request, responce);
	}
}
